/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.dataprovider.view.suite.panels.filetree;

import com.compomics.dataprovider.view.suite.model.FastaRepositoryProperties;
import java.io.File;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author Kenneth
 */
public class FastaFileTree extends JTree {

    public void updateTree(File node) {
        DefaultTreeModel model = new DefaultTreeModel(scan(node));
        setModel(model);
    }

    public File getSelectedFolder() {
        File selectedFolder = new File(FastaRepositoryProperties.getRepositoryPath());
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) getLastSelectedPathComponent();
        if (node != null && node.getLevel() == 1) {
            String foldername = node.toString().replace(".fasta", "").replace(".FASTA", "");
            selectedFolder = new File(selectedFolder, foldername);
            if (node.getLevel() == 0) {
                foldername = node.toString().replace(".fasta", "").replace(".FASTA", "");
                selectedFolder = new File(selectedFolder, foldername);
            }
        }
        return selectedFolder;
    }

    private DefaultMutableTreeNode scan(File node) {
        DefaultMutableTreeNode ret = new DefaultMutableTreeNode(node.getName());
        if (node.isDirectory()) {
            for (File child : node.listFiles()) {
                ret.add(scan(child));
            }
        }
        return ret;
    }

}
